/**
 * Include Standard Libraries
**/
#include <iostream>

/**
 * Associated header
**/
#include <arguments.h>

/**
 *  Memory used to store parameter values
**/
char param_baseScript[1024] = "";      /** The base lua script file location **/

/**
 *  This structure stores information to interpret an argument/parameter
 *  - format is a scanf format string
 *  - pointer is the memory location to store the value
**/
typedef struct {
    const char * format;
    void       * pointer;
} tParams;

/**
 * param_choices stores the parameter format/pointer pairs
 *  for later iteration
**/
#define NUM_PARAM_CHOICES 1
const tParams param_choices[NUM_PARAM_CHOICES] = {
    {"-base=%s", (void*)param_baseScript}
};

/**
 * Parse the arguments given to the program
**/
tProgramState parse_args(int argc, const char * argv[]) {
    /**
     * used to find valid parameters
    **/
    const char   ** argi;
    const tParams * parami;

    /**
     * For every argument passed to the program except the first
    **/
    for ( argi = &argv[1];
          argi < &argv[argc];
          ++argi)
    {
        /**
         * If help is requested return
        **/
        if ( strcmp(*argi, "--help") == 0 )
            return PROGRAM_STATE_DISPLAY_HELP;

        /**
         * If the version number is requested return
        **/
        if ( strcmp(*argi, "--version") == 0 )
            return PROGRAM_STATE_DISPLAY_VERSION;

        /**
         * Compare every argument with every valid parameter
        **/
        for ( parami = &param_choices[0];
              parami < &param_choices[NUM_PARAM_CHOICES];
              ++parami)
        {
            /**
             * If the parameter was successfully retrieved, go to the next
            **/
            if ( sscanf(*argi, parami->format, parami->pointer) == 1 )
                break;
        }
        /**
         * If the argument is still unresolved display an error
        **/
        if (parami == &param_choices[NUM_PARAM_CHOICES])
            std::cerr << "Unknown parameter: '" << *argi << "' !" << std::endl;
    }

    /**
     * Return success
    **/
    return PROGRAM_STATE_CONTINUE;
}
